package tinycloud.gui.action;


import tinycloud.gui.dialog.PreferencesDialog;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import tinycloud.gui.dialog.PreferencesDialog;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nicolas
 */
public class AddDirectoryPreferencesDialogAction extends AbstractAction {

    private PreferencesDialog dialog;

    public AddDirectoryPreferencesDialogAction(PreferencesDialog dialog) {
        this.dialog = dialog;
    }

    public void actionPerformed(ActionEvent e) {
        //dialog.list.add
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.showOpenDialog(fc);
        dialog.listModel.addElement(fc.getSelectedFile().toString());
        Connection c;
        try {
            Class.forName("org.hsqldb.jdbcDriver").newInstance();
            c = DriverManager.getConnection("jdbc:hsqldb:file:tinycloud", "sa", "");
            Statement s = c.createStatement();
            s.executeUpdate("INSERT INTO directories VALUES ('" + fc.getSelectedFile().toString() + "', 0)");
            s.executeQuery("SHUTDOWN");
            s.close();
            c.close();
        } catch (Exception ex) {
            System.out.print(ex);
        }
    }    
}
